package pers.leeyns.cms.baidu;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import pers.leeyns.cms.utils.HTTP;


public class Connection {

	public static HttpResponse get(String url,Map<String,Object> params,Map<String,Object> headers) throws Exception{
		System.out.println("URL:"+url);
		
		HttpClient hc = HttpClients.createDefault();
		StringBuffer sbf = new StringBuffer(url);
		
		if(params!= null && !params.isEmpty()){
			for(Entry<String,Object> e:params.entrySet()){
				sbf.append(e.getKey()).append("=").append(e.getValue().toString()).append("&");
			}
		}
		String uri = sbf.toString().substring(0,sbf.length()-1);
		
		HttpGet get = new HttpGet(uri);
		if(headers != null && !headers.isEmpty()){
			for(Entry<String,Object> e:headers.entrySet()){
				get.addHeader(e.getKey(), e.getValue().toString());
			}
		}
		get.addHeader("Host","passport.baidu.com");
		get.addHeader("User-Agent","Firefox/45.0");
		get.addHeader("Referer","http://yun.baidu.com/");
		return hc.execute(get);
	}
	public static HttpResponse post(String url){
		try {
			return post(url, null, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static HttpResponse post(String url,Map<String,Object> getParams,List<NameValuePair> postParamList,Map<String,Object> headers) throws Exception{
		HttpClient hc = HttpClients.createDefault();
		StringBuffer sbf = new StringBuffer(url);
		
		if(getParams!= null && !getParams.isEmpty()){
			for(Entry<String,Object> e:getParams.entrySet()){
				sbf.append(e.getKey()).append("=").append(e.getValue().toString()).append("&");
			}
		}
		String uri = sbf.toString();
		if(uri.endsWith("&")){
			uri = uri.substring(0,uri.length()-1);
		}
		
		HttpPost post = new HttpPost(uri);
		if(headers != null && !headers.isEmpty()){
			for(Entry<String,Object> e:headers.entrySet()){
				post.addHeader(e.getKey(), e.getValue().toString());
			}
		}
		post.addHeader("Host","passport.baidu.com");
		post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
		post.addHeader("Referer","http://yun.baidu.com/");
		if(postParamList != null && postParamList.size() > 0){
			post.setEntity(new UrlEncodedFormEntity(postParamList,Charset.forName("UTF-8")));
		}
		return hc.execute(post);
	}
	
	public static String responseToString(HttpResponse resp) throws Exception{
		if(resp == null) return null;
		return EntityUtils.toString(resp.getEntity());
	}
	
	public static String getCookie(String url){
		
		try {
			HttpResponse resp = get(url, null, null);
			
			Header[] cookie = resp.getHeaders(HTTP.HeaderNames.SET_COOKIE);
			StringBuffer sbf = new StringBuffer();
			if(cookie != null && cookie.length > 0){
				for(Header c:cookie){
					sbf.append(c.getValue()).append(";");
				}
			}
			return sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getCookie(HttpResponse resp) {

		try {
			Header[] cookie = resp.getHeaders(HTTP.HeaderNames.COOKIE);
			StringBuffer sbf = new StringBuffer();
			if (cookie != null && cookie.length > 0) {
				for (Header c : cookie) {
					sbf.append(c.getValue()).append(";");
				}
			}
			return sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
